/************** init.html **************/
function query_vehicle(page) {
	var parm = $('#queryVehicleForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('vehicle-query');
	sys_record_parm('vehicle',parm);
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#vehicleResultList').html(result);
		}
	});
}


function delete_vehicle(code) {
	$.Zebra_Dialog('确认要删除 文档类别信息吗？',{
	'type': 'question','title': '警告' ,'buttons':[
       {caption:'确认',callback:function() {
			var url = create_url('vehicle-delete');
			var parm = {'code':code};
			sys_submiting();
			$.ajax({type:'POST', url:url, data:parm,
				success: function(result) {
					sys_submiting_close();
					aidQuery_vehicle();
				}
			})}},
       {caption:'取消',callback:function(){}
		}]
	});
}

function save_vehicle() {
	var parm = $('#editVehicleForm').serialize();
	var url = create_url('vehicle-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if (result == 'success') {
				sys_load_handle('vehicle');
			}else if (result == 'null_type'){
				sys_msg('车辆类型为空');
			}else if (result == 'null_plate'){
				sys_msg('车牌号不能为空');
			}else if (result == 'null_price'){
				sys_msg('车牌价格不能为空');
			}else if (result == 'null_name'){
				sys_msg('车辆名称不能为空');
			}else {
				sys_msg('操作失败!');
			}
		}
	});
}


function selectApply_vehicle() {
	var url = create_url('vehicle-aidInit1');
	$.colorbox({overlayClose:false, href:url, close:'[关闭]', width:'900px', height:'700px', open:true, 
		onClosed:function(){
		}
	});
}


function choose_vehicle(code) {
	$.fn.colorbox.close();
	sys_load_handle('vehicle-assign_' + code);
}


function aidQuery_vehicle(page) {
	sys_loading();
	var parm = $('#aidQueryForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('vehicle-aidList1');
	$.ajax({type:'POST', url:url, data:parm,success: function(result) {
		sys_loading_close();
		$('#aidResultList').html(result);
		}
	});
}

/******************************userInit****************************/
function selectVehicle_vehicle(k) {
	var suc = '';
	$('#userListTd').find('.ucs').each(function () {
		suc += $(this).val() + ',';
	});
	if ('' != suc) {
		suc = suc.substring(0, suc.length - 1);
	}
	var parm = {'suc':suc, "p":k};
	var url = create_url("vehicle-aidInit2");
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'1000px', height:'700px', open:true});
}

function aidQueryVehicle_vehicle(page) {
	var suc = '';
	$('#selectedUserListBody').find('.run').each(function () {
		suc += $(this).val() + ',';
	});
	if (suc.length > 0) {
		suc = suc.substring(0, suc.length - 1);
	}
	var parm = $('#aidQueryForm').serialize() + '&suc=' + suc + '&pageSize=10' + (page != null ? ('&page=' + page) : '');
	var url = create_url('vehicle-aidList2');

	sys_loading();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#aidQueryResultList').html(result);
		}
	});
}

function chooseVehicle_vehicle() {
	var k = $('#u_p').val();
	var template = $('#tempList').find('.rep');
	var userListTd = $('#vaList_' + k);
	var i = 0;
	userListTd.empty();
	$('#selectedUserListBody').find('.rep').each(function () {
		var rep = template.clone(true);
		rep.find('.title').html($(this).find('.un').val()).end()
			.find(".pc").attr({"name" : "va", "value":$(this).find('.run').val()}).end()
			.find('.em').val($(this).find('.em').val()).end()
			.find('.ucs').attr({'name':"vaList[" + i + "].mc"}).end()
			.find('.cn').attr({'name': 'vaList[" + i + "].cn'}).end()
			.find('.btn').attr({'pib': i}).end()
			.find('.cu').attr({'name': 'vaList[" + i + "].cu'}).end()
			.find('.skm').attr({'name': 'vaList[" + i + "].skm'}).end();
		userListTd.append(rep);
		i++;
	});
	
	userListTd.parents('.td-rep').css('display','');
	$.fn.colorbox.close();
}

function deleteVehicle_vehicle(btn) {
	btn.parents('.rep').remove();
	aidQueryVehicle_vehicle($('#aidQueryUserPageTxt').val());
}

function add_vehicle(code, name, on, plate) {
	var tr = $('#tempUserTable').find('.rep').clone(true);
	tr.find('.rn').html(name).end()
		.find('.on').html(on).end()
		.find('.run').val(code).end()
		.find('.emt').html(plate).end()
		.find('.un').val(name).end()
		.find('.em').val(plate).end();
	$('#selectedUserListBody').append(tr);
	aidQueryVehicle_vehicle($('#aidQueryUserPageTxt').val());
}
function saveAssign_vehicle() {
	var cu = $('#user').val();
	var pwd = $('#pwd').val();
	var url =  create_url('vehicle-affirm');
	var parm = {'cu':cu,'pwd':pwd};
	$.ajax({type:'POST', url:url, data:parm,success : function(result) {
		if ('success' == result) {
			var parm = $('#editVehicleForm').serialize();
			var url = create_url('vehicle-saveAssign');
			sys_submiting();
			$.ajax({type:'POST', url:url, data:parm,
				success: function(result) {
					sys_submiting_close();
					if (result == 'success') {
						sys_load_handle('vehicle');
					} else if (result == 'exists'){
						sys_msg('没有指派车辆呢');
					} else {
						sys_msg('操作失败!');
					}
				}
			});
		} else if ('parameter_error') {
			sys_msg('密码或用户名为空！');
		} else if ('exists' == result) {
			sys_msg('密码错误！');
		} else {
			sys_msg('验证失败！');
		}
	}
	});
	
}


//密码确认
function affirm_vehicle() {
	var cu = $('#user').val();
	var pwd = $('#pwd').val();
	var url =  create_url('vehicle-affirm');
	var parm = {'cu':cu,'pwd':pwd};
	$.ajax({type:'POST', url:url, data:parm,success : function(result) {
		if ('success' == result) {
			sys_msg('密码验证成功！');
		} else if ('parameter_error') {
			sys_msg('密码或用户名为空！');
		}else if ('exists' == result) {
			sys_msg('密码错误！');
		} else {
			sys_msg('验证失败！');
		}
	}
	});
}

function select_vehicle() {
	var a = $('#plate').val();
	var parm ={'plate':a};
	var url = create_url('vehicle-aidInit');
	$.colorbox({overlayClose:false, data:parm, href:url, close:'[关闭]', width:'400px', height:'300px', open:true, 
		onClosed:function(){
		}
	});
}
function returnInfo_vehicle() {
	var a = $('#ekm').val();
	if (a == null || a ==''){
		sys_msg('公里数还没填呢！');
		return false;
	}
	var cu = $('#user').val();
	var pwd = $('#pwd').val();
	var url =  create_url('vehicle-affirm');
	var parm = {'cu':cu,'pwd':pwd};
	$.ajax({type:'POST', url:url, data:parm,success : function(result) {
		if ('success' == result) {
			$.fn.colorbox.close();
			var b = $('#plate').val();
			var parm ={'ekm' :a,'plate': b};
			var url =  create_url('vehicle-returnInfo');
			$.ajax({type:'POST', url:url, data:parm,success : function(result) {
				if ('success' == result) {
					sys_msg('操作成功！');
					sys_load_handle('vehicle');
				} else {
					sys_msg('操作失败！');
				}
				}
			});
		} else if ('parameter_error') {
			sys_msg('密码或用户名为空！');
		}else if ('exists' == result) {
			sys_msg('密码错误！');
		} else {
			sys_msg('验证失败！');
		}
	}
	});
}